//
//  BKCMCellAmountView.m
//  bestkeep
//
//  Created by 毛韶谦 on 2017/1/16.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKCMCellAmountView.h"
#import "BKSCCommon.h"
#import "BKCMGoodsDetailModel.h"

#define MARGIN 10
#define ONLYNum @"0123456789"

@interface BKCMCellAmountView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *big_button1;  //覆盖减号上，用于扩大点击范围；
@property (nonatomic, strong) UIButton *big_button2;

@end

@implementation BKCMCellAmountView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES;
        
        
        //减号
        _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button1 setImage:[UIImage imageNamed:@"BKSCjian_hei"] forState:UIControlStateNormal];
        [_button1 setImage:[UIImage imageNamed:@"BKSCjian_hui"] forState:UIControlStateSelected];
        
        _button1.layer.borderWidth = 1.0;
        _button1.layer.borderColor = [BKSCCommon fromHexValue:0x888888].CGColor;
        [self addSubview:_button1];
        
        
        _label1 = [UITextField new];
        [_label1 setText:@" -- "];
        _label1.delegate = self;
        [_label1 addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
        
        [_label1 setFont:[UIFont systemFontOfSize:13.0]];
        [_label1 setTextAlignment:NSTextAlignmentCenter];
        
        UIView *keyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 40)];
        keyView.backgroundColor = RGBACOLOR(236, 236, 236, 1.0);
        //        UIButton *finishButton = [UIButton new];
        
        UIButton *cancleButton = [[UIButton alloc] initWithFrame:CGRectMake(MARGIN, MARGIN, 50, 20)];
        
        [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancleButton setTitleColor:[BKSCCommon fromHexValue:0x888888] forState:UIControlStateNormal];
        [cancleButton addTarget: self action:@selector(cancleKeyBoard:) forControlEvents:UIControlEventTouchUpInside];
        [keyView addSubview:cancleButton];
        
        UIButton *finishButton = [[UIButton alloc] initWithFrame:CGRectMake(IPHONE_WIDTH - 6 * MARGIN, MARGIN, 50, 20)];
        [finishButton setTitle:@"完成" forState:UIControlStateNormal];
        [finishButton setTitleColor:COLOR_04 forState:UIControlStateNormal];
        [finishButton addTarget:self action:@selector(finishKeyBoard:) forControlEvents:UIControlEventTouchUpInside];
        
        [keyView addSubview:finishButton];
        
        _label1.inputAccessoryView = keyView;
        _label1.returnKeyType = UIReturnKeyDone;
        _label1.keyboardType = UIKeyboardTypeNumberPad;
        
        _label1.layer.borderWidth = 1.0;
        _label1.layer.borderColor = [BKSCCommon fromHexValue:0x888888].CGColor;
        [_label1 setTextColor:RGB(64, 64, 64)];
        [self addSubview:_label1];
        
        _button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button2 setImage:[UIImage imageNamed:@"BKSCjia_hei"] forState:UIControlStateNormal];
        [_button2 setImage:[UIImage imageNamed:@"BKSCjia_hui"] forState:UIControlStateSelected];
        
        _button2.layer.borderWidth = 1.0;
        _button2.layer.borderColor = [BKSCCommon fromHexValue:0x888888].CGColor;
        [self addSubview:_button2];
        
        [_button1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(22.5f));//19--24---30---25
            make.bottom.mas_equalTo(self.mas_bottom);
            make.width.mas_equalTo(@(23));//18--23----29
        }];
        [_button2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).mas_offset(-10);
            make.height.mas_equalTo(@(22.5f));//19--24---30--25
            make.bottom.mas_equalTo(self.mas_bottom);
            make.width.mas_equalTo(@(23));//18--23---29
        }];
        
        
        
        
        [_label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_button1.mas_right).offset(-1);
            make.right.mas_equalTo(_button2.mas_left).offset(1);
            make.bottom.mas_equalTo(self.mas_bottom);
            make.height.mas_equalTo(@(22.5));//19--24---30--25
            make.width.mas_equalTo(28);
        }];
        
        _big_button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _big_button1.backgroundColor = [UIColor clearColor];
        [self addSubview:_big_button1];
        [_big_button1 addTarget:self action:@selector(button1ClickWithText:) forControlEvents:UIControlEventTouchUpInside];
        [_big_button1 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.mas_left);
            make.right.mas_equalTo(self.button1.mas_right);
            make.bottom.mas_equalTo(self.button1.mas_bottom);
            make.top.mas_equalTo(self.mas_top);
        }];
        _big_button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        _big_button2.backgroundColor = [UIColor clearColor];
        [self addSubview:_big_button2];
        [_big_button2 addTarget:self action:@selector(button2ClickWithText:) forControlEvents:UIControlEventTouchUpInside];
        [_big_button2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.button2.mas_left);
            make.right.mas_equalTo(self.mas_right);
            make.bottom.mas_equalTo(self.button2.mas_bottom);
            make.top.mas_equalTo(self.mas_top);
        }];
    }
    return self;
}
// -
- (void)button1ClickWithText:(UITextField *)textField{
    
    
    textField = _label1;
    if ([textField.text integerValue] == 1) {
        return;
    }
    if ([self.dataViewDelegate respondsToSelector:@selector(cellDataView:subCount:)]) {
        [self.dataViewDelegate cellDataView:self subCount:textField];
    }
    
}
// +
- (void)button2ClickWithText:(UITextField *)textField{
    
    
    textField = _label1;
    
    if ([self.dataViewDelegate respondsToSelector:@selector(cellDataView:addCount:)]) {
        [self.dataViewDelegate cellDataView:self addCount:textField];
    }
    
}

-(void)updateDataWithShoppingCarList:(BKCMGoodsDetailModel *)orderModel{
    self.label1.text = orderModel.amount;
}
// KeyBoard右边完成按钮
- (void)finishKeyBoard:(UITextField *)sender{
    NSLog(@"--163-->finishKeyBoard");
    
    
    sender = _label1;
    
    if ([sender.text integerValue] > 200) {
        
        NSLog(@"--185-->[sender.text integerValue] > 200");
        
        [self.dataViewDelegate cellDataView:self updateButtonClick:sender];
    }else{
        
        [self.dataViewDelegate cellDataView:self updateButtonClick:sender];
        [_label1 resignFirstResponder];
    }
    
    
}
// 第三方键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    textField = _label1;
    
    if ([textField.text integerValue] > 200) {
        
        NSLog(@"--207-->[sender.text integerValue] > 200");
        
        [self.dataViewDelegate cellDataView:self updateButtonClick:textField];
    }else{
        
        [self.dataViewDelegate cellDataView:self updateButtonClick:textField];
        [_label1 resignFirstResponder];
    }
    return YES;
    
}
// 监听输入数值
- (void)textValueChanged:(UITextField *)textField
{
    NSLog(@"%@",textField.text);
    if ([textField.text integerValue]>200) {
        
        [self.dataViewDelegate cellDataView:self updateButtonClick:textField];
        
    }
    
    
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    cs = [[NSCharacterSet characterSetWithCharactersInString:ONLYNum] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
    BOOL canChange = [string isEqualToString:filtered];
    return canChange;
}


// 取消KeyBoard返回最先的textfiled数值
- (void)cancleKeyBoard:(UITextField *)textField{
    NSLog(@"--168-->cancleKeyBoard");
    
    textField = _label1;
    [self.dataViewDelegate cellDataView:self cancleButtonClick:textField];
    
    [_label1 resignFirstResponder];
}
- (void)setAmount:(NSString *)amount{
    if ([amount isEqualToString:@"0"]) {
        amount = @"1";
    }
    _label1.text = [NSString stringWithFormat:@"%@",amount];
    
    if ([amount isEqualToString:@"1"]) {
        
        _button1.selected = YES;
    }else {
        _button1.selected = NO;
    }
    
    if ([amount intValue] >= 200) {
        
        _button2.selected = YES;
    }else {
        _button2.selected = NO;
    }
}
- (void)setTempStr:(NSString *)tempStr {
    
    if ([tempStr isEqualToString:@""]) { // 减号变色
        _label1.textColor = [BKSCCommon fromHexValue:0xB2B2B2];
        _button2.layer.borderColor = [BKSCCommon fromHexValue:0xB2B2B2].CGColor;
        _button1.layer.borderColor = [BKSCCommon fromHexValue:0xB2B2B2].CGColor;
        _label1.layer.borderColor = [BKSCCommon fromHexValue:0xB2B2B2].CGColor;
        _big_button1.userInteractionEnabled = NO;
        _big_button2.userInteractionEnabled = NO;
        _button2.userInteractionEnabled = NO;
        _button1.userInteractionEnabled = NO;
        _label1.userInteractionEnabled = NO;
        [_button1 setImage:[UIImage imageNamed:@"BKSCjian_hui"] forState:UIControlStateNormal];
        [_button2 setImage:[UIImage imageNamed:@"BKSCjia_hui"] forState:UIControlStateNormal];
        
    }else {
        _label1.textColor = [UIColor blackColor];
        _button2.layer.borderColor = [BKSCCommon fromHexValue:0x888888].CGColor;
        _button1.layer.borderColor = [BKSCCommon fromHexValue:0x888888].CGColor;
        _label1.layer.borderColor = [BKSCCommon fromHexValue:0x888888].CGColor;
        [_button1 setImage:[UIImage imageNamed:@"BKSCjian_hei"] forState:UIControlStateNormal];
        [_button2 setImage:[UIImage imageNamed:@"BKSCjia_hei"] forState:UIControlStateNormal];
        _big_button1.userInteractionEnabled = YES;
        _big_button2.userInteractionEnabled = YES;
        _button2.userInteractionEnabled = YES;
        _button1.userInteractionEnabled = YES;
        _label1.userInteractionEnabled = YES;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
